package time;

public class Heure implements Comparable {
	private int h=0;
	private int m=0;
	private int s=0;
	private int format=24;
	private boolean am=true;
	private boolean pm=false;
	
	
	public Heure(int h, int m, int s) {
		this.h=h;
		this.m=m;
		this.s=s;
		majAmPm();
	}
	
	public Heure(int h, int m) {
		this.h=h;
		this.m=m;
		majAmPm();
	}
	
	public Heure(int h) {
		this.h=h;
		majAmPm();
	}
	
	public Heure() {
	}
	
	public Heure(Heure h) {
		this.h = h.h;
		this.m = h.m;
		this.s = h.s;
		this.format = h.format;
		this.am = h.am;
		this.pm = h.pm;
	}
	
	public void majAmPm() {
		if(format == 12) {
			if(h>12) {
				h=(h%12);
				pm=true;
				am=false;
			}
		}
	}
	
	public void switchAmPm() {
		if(pm) {
			pm=false;
			am=true;
		} else {
			pm=true;
			am=false;
		}
	}
	
	public void addHeure(int heure) {
		h+=heure;
		if(format == 12 && h>12) switchAmPm();
		h=(h%format);
			
	}
	
	public void addMinute(int minute) {
		m+=minute;
		if(m>59) addHeure(m/60);
		m=m%60;	
	}
	
	public void addSeconde(int seconde) {
		s+=seconde;
		if(s>59) addMinute(s/60);
		s=s%60;	
	}
	
	public void remHeure(int heure) {
		h-=heure;
		if(h<0){
			if(format == 12) switchAmPm();
			h+=format;
		}
			
	}
	
	public void remMinute(int minute) {
		m-=minute%60;
		if(minute%60 == 0) {
			remHeure(minute/60);
		}
		if(m<0) {
			remHeure(minute/60+1);
			m=m+60;
		}
	}
	
	public void remSeconde(int seconde) {
		s-=seconde%60;
		if(seconde%60 == 0) {
			remMinute(seconde/60);
		}
		if(s<0) {
			remMinute(seconde/60+1);
			s=s+60;
		}
		
	}
	
	public String toString() {
		String res;
		res=String.valueOf(h)+":"+String.valueOf(m)+":"+String.valueOf(s);
		if(format == 12 ) {
			if(am) res+=" AM";
			else res+=" PM";
		}
		return res;
	}
	
    public void addDuree(Heure h) {
        addSeconde(h.getS());
        addMinute(h.getM());
        addHeure(h.getH());
    }
    
    public void removeDuree(Heure h) {
        remSeconde(h.getS());
        remMinute(h.getM());
        remHeure(h.getH());
    }
    
    public static Heure opPlus(Heure h1, Heure h2) {
    	Heure res = new Heure(h1);
    	res.addDuree(h2);
    	return res;
    }
    
    public static Heure opMoins(Heure h1, Heure h2) {
    	if(h1.compareTo(h2)<0) {
    		Heure res = new Heure(h2);
        	res.removeDuree(h1);
        	return res;
    	} else {
	    	Heure res = new Heure(h1);
	    	res.removeDuree(h2);
	    	return res;
    	}
    }

	
	public int getH() {
		return h;
	}

	public void setH(int h) {
		this.h = h;
	}

	public int getM() {
		return m;
	}

	public void setM(int m) {
		this.m = m;
	}

	public int getS() {
		return s;
	}

	public void setS(int s) {
		this.s = s;
	}

	public int getFormat() {
		return format;
	}

	public void setFormat(int format) {
		this.format = format;
	}

	public boolean isAm() {
		return am;
	}

	public void setAm(boolean am) {
		this.am = am;
	}

	public boolean isPm() {
		return pm;
	}

	public void setPm(boolean pm) {
		this.pm = pm;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Heure h = new Heure(4, 15, 8);
		System.out.println(h);
		h.removeDuree(new Heure(5, 45, 9));
		System.out.println(opMoins(new Heure(18,15), new Heure(16,30)));
	}

	@Override
	public int compareTo(Object o) {
		if(o instanceof Heure) {
			Heure heure = (Heure)o;
			if(format == 12 && heure.format==12 && am && heure.pm) return -1; //Format 24
			else if(format == 12 && heure.format==12 && pm && heure.am) return 1;
			else {
				if(h<heure.getH()) return -1; //Heure inférieure
				else if(h == heure.getH()){ //Heure égale
					if(m<heure.getM()) return -1; //Minute inférieure
					else if(m == heure.getM()) { //Minute égale
						if(s < heure.getS()) return -1; //Seconde inférieure
						else if (s == heure.getS()) return 0; //Seconde égale
						else return 1; //Seconde supérieure
					} else return 1; //Minute supérieure
				} else return 1; //Heure supérieure
			}
		}
		return 0;
	}
	
	@Override public boolean equals(Object o) {
		if(o instanceof Integer && ((Integer)o).equals(0)) return (h == 0 && m == 0 && s == 0);
		else if (o instanceof Heure) {
			Heure h2 = (Heure)o;
			return (h == h2.h && m == h2.m && s == h2.s);
		}
		else return false;
	}


}
